package org.itboys.kitchen.util;


import org.apache.commons.lang3.StringUtils;
import java.util.Date;


/**
 * 包名：com.hz.sunday.xlcl.utils
 * 作者 : 江雄
 * Email: empty90911@126.com
 * 时间：2016年1月3日 下午4:10:43 
 * 描述:编号生成器
 *
 */
public class NumberUtil {
	/**
	 * 生成编号
	 * @return
	 */
	public static String generateNumber(){
		StringBuilder orderNo =new StringBuilder();
		Date now = new Date();
		String year = String.valueOf(now.getYear()+1900);
		String month= (now.getMonth()+1)<10?"0"+String.valueOf(now.getMonth()+1):String.valueOf(now.getMonth()+1);
		String day = now.getDate()<10?"0"+String.valueOf(now.getDate()):String.valueOf(now.getDate());
		//年月日
		orderNo.append(year);
		orderNo.append(month);
		orderNo.append(day);
		//毫秒时间戳后6位
		String times = String.valueOf(new Date().getTime());
		times=times.substring(times.length()-4,times.length());
		orderNo.append(times);
		//X位随机数
		String fourNumber = RandomUtil.generateRandomNumber(3);
		orderNo.append(fourNumber);
		return orderNo.toString();
	}
	/**
	 * 生成编号
	 * @return
	 */
	public static String generateNumber(String prefix){
		StringBuilder orderNo =new StringBuilder();
		Date now = new Date();
		String year = String.valueOf(now.getYear()+1900);
		String month= (now.getMonth()+1)<10?"0"+String.valueOf(now.getMonth()+1):String.valueOf(now.getMonth()+1);
		String day = now.getDate()<10?"0"+String.valueOf(now.getDate()):String.valueOf(now.getDate());
		if(StringUtils.isNotEmpty(prefix)){
			orderNo.append(prefix);
		}

		//年月日
		orderNo.append(year);
		orderNo.append(month);
		orderNo.append(day);
		//毫秒时间戳后6位
		String times = String.valueOf(new Date().getTime());
		times=times.substring(times.length()-4,times.length());
		orderNo.append(times);
		//6位随机数
		String fourNumber = RandomUtil.generateRandomNumber(6);
		orderNo.append(fourNumber);
		return orderNo.toString();
	}
}
